<?php

namespace App\Models\Payment;

use App\Models\Common\Constant;
use App\Traits\UuidTrait;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Database\Eloquent\Relations\MorphTo;

/**
 * App\Models\Payment\PaymentOrder
 *
 * @property string $id
 * @property string $parking_lot_id
 * @property string $related_id
 * @property string $related_type
 * @property string $order_title
 * @property string $paid_code
 * @property int $amount
 * @property int $balance_amount
 * @property int $point_amount
 * @property int $frozen_balance_amount
 * @property int $frozen_point
 * @property string|null $paid_at
 * @property string $out_trade_no
 * @property string $pay_type
 * @property string $trade_type
 * @property string $order_status
 * @property array|null $snapshot
 * @property int $had_invoiced
 * @property string $user_id
 * @property string $user_name
 * @property string $payment_channel_id
 * @property string $payer_id
 * @property string $error
 * @property array|null $optional
 * @property string|null $created_at
 * @property \Illuminate\Support\Carbon|null $updated_at
 * @property-read \App\Models\Payment\PaymentChannel|null $paymentChannel
 * @property-read Model|\Eloquent $related
 * @method static \Illuminate\Database\Eloquent\Builder|PaymentOrder newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|PaymentOrder newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|PaymentOrder query()
 * @method static \Illuminate\Database\Eloquent\Builder|PaymentOrder whereAmount($value)
 * @method static \Illuminate\Database\Eloquent\Builder|PaymentOrder whereBalanceAmount($value)
 * @method static \Illuminate\Database\Eloquent\Builder|PaymentOrder whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|PaymentOrder whereError($value)
 * @method static \Illuminate\Database\Eloquent\Builder|PaymentOrder whereFrozenBalanceAmount($value)
 * @method static \Illuminate\Database\Eloquent\Builder|PaymentOrder whereFrozenPoint($value)
 * @method static \Illuminate\Database\Eloquent\Builder|PaymentOrder whereHadInvoiced($value)
 * @method static \Illuminate\Database\Eloquent\Builder|PaymentOrder whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|PaymentOrder whereOptional($value)
 * @method static \Illuminate\Database\Eloquent\Builder|PaymentOrder whereOrderStatus($value)
 * @method static \Illuminate\Database\Eloquent\Builder|PaymentOrder whereOrderTitle($value)
 * @method static \Illuminate\Database\Eloquent\Builder|PaymentOrder whereOutTradeNo($value)
 * @method static \Illuminate\Database\Eloquent\Builder|PaymentOrder wherePaidAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|PaymentOrder wherePaidCode($value)
 * @method static \Illuminate\Database\Eloquent\Builder|PaymentOrder whereParkingLotId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|PaymentOrder wherePayType($value)
 * @method static \Illuminate\Database\Eloquent\Builder|PaymentOrder wherePayerId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|PaymentOrder wherePaymentChannelId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|PaymentOrder wherePointAmount($value)
 * @method static \Illuminate\Database\Eloquent\Builder|PaymentOrder whereRelatedId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|PaymentOrder whereRelatedType($value)
 * @method static \Illuminate\Database\Eloquent\Builder|PaymentOrder whereSnapshot($value)
 * @method static \Illuminate\Database\Eloquent\Builder|PaymentOrder whereTradeType($value)
 * @method static \Illuminate\Database\Eloquent\Builder|PaymentOrder whereUpdatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|PaymentOrder whereUserId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|PaymentOrder whereUserName($value)
 * @mixin \Eloquent
 */
class PaymentOrder extends Model
{
    use UuidTrait;

    const TABLE = 'payment_orders';
    protected $table = self::TABLE;

    public $casts = [
        'created_at' => 'string', // datetime:Y-m-d H:i:s
        'snapshot' => 'array',
        'optional' => 'array',
    ];

    public $attributes = [
        'snapshot' => '[]',
        'optional' => '[]',
        'order_status' => Constant::ORDER_STATUS_UNPAID
    ];

    public function related(): MorphTo
    {
        return $this->morphTo();
    }

    public function paymentChannel():HasOne
    {
        return $this->hasOne(PaymentChannel::class,'id','payment_channel_id');
    }
}
